<extend name="Base/common" />
<block name="body">
	<!--导航引用-->
	<include file="Public/nav" />
	<footer class="ui-footer ui-footer-stable ui-border-t">
			
		<div class="ui-cart-pay ui-tiled">
			<div class="ui-cart-goods-amount">
				<input type="hidden" value="{$data.cartAllPrice|default=0}" id="allprice">
				<input type="hidden" value="{$data.saveMoney|default=0}" id="saveMoney">
				<span>总金额：<b>￥{$data.cartAllPrice|number_format=###,2,'.',''}</b><i class="ui-goods-cart-discount">(立奖：{$data.saveMoney|number_format=###,2,'.',''})</i></span>
			</div>
			<div class="ui-cart-pay-btn ui-btn-group">
			    <button class="ui-btn ui-btn-danger" id="cartCount">结算</button>
			</div>
		</div>
	    
	</footer>
	<section class="ui-container">
		<form id="cartForm" action="{:U('Order/order_index')}" method="post">
			<ul class="ui-list-goods-cart ui-whitespace">
				<empty name="list">
				<li>
					<!--s not goods-->
					<section class="ui-not-goods-wrap">
						<div class="ui-cart-not-goods">
						    <i class="iconfont icon-cart"></i>
						    <p>没有商品噢(ㄒoㄒ)~~</p>
						    <div class="ui-btn-group">
						        <a class="ui-btn-primary ui-btn" onclick="goHome();">去选购</a>
						    </div>
					    </div>
					</section><!--e not goods-->
				</li>
				<else />
					<volist name="list" id="v">
						<li class="ui-tiled ui-border-b <eq name='key' value='0'>checked</eq>">
							<label class="ui-checkbox-s">
						        <input type="radio" id="choosebox" name="checkbox" value="{$v.id}" <eq name="key" value="0">checked</eq>>
						    </label>
							<div class="ui-list-goods-img">
					            <img src="{$v.picture||get_cover='path'}" width="50" height="50"/>
					        </div>
					        <div class="ui-list-goods-info">
					        	<h5>{$v.name}</h5>
					        	
					        	<div class="count ui-tiled ui-border">
					        		<a href="#" class="move iconfont ui-border-r">&#xe768;</a>
					        		<span class="number" did="{$v.id}" zhekou="{$v.zhekou}" price="{$v.price}" isppl="{$v.is_ppl}">{$v.num}</span>
					        		<a href="#" class="add iconfont ui-border-l">&#xe767;</a>
					        	</div>
					        </div>
					        <div class="ui-list-goods-price">
					        	<p class="price">
					        		<b>￥{$v.price|number_format=###,2,'.',''}</b>
					        		<!-- <del>{$v.shop_price}</del> -->
					        		<b class="ui-txt-highlight">立奖：{$v.zhekou|count_zhekou=###}%</b>
					        	</p>
						        <i class="iconfont ui-goods-cart-close" did="{$v.id}" zhekou="{$v.zhekou}" price="{$v.price}" num="{$v.num}">&#xe646;</i>
					        </div>
						</li>
					</volist>
				</empty >
			</ul>
			<input type="hidden" name="id" id="id" value="">
		</form>
		<div class="ui-cart-hint">
			<p>支付问题，请咨询点粉圈：<font color="#F04E23">400-156-8848</font></p>
			<!-- <p>产品问题，请咨询厂家：<font color="#0080FF">{$data.class_id|getShopPhone=###}</font></p> -->
		</div>
		
	</section>
</block>
<block name="script">
	<script type="text/javascript">
		$('#id').val($('.ui-list-goods-cart li').eq(0).find('label input').val());
		var UNUM = $('.ui-list-goods-cart li .ui-list-goods-info div a');
		var DEL = $('.ui-list-goods-cart li .ui-list-goods-price i');
		var js_price = parseFloat($('#allprice').val());
		var js_zhekou = parseFloat($('#saveMoney').val());
			
		//数量增加或减少
		UNUM.tap(function(){
			var obj = $(this).parent().find('span');
			var num = Number(obj.html());
			var did = Number(obj.attr('did'));
			var zhekou = Number(obj.attr('zhekou'))/100*0.4;
			var price = parseFloat(obj.attr('price'));
			var is_ppl = Number(obj.attr('isppl'));
			
			console.log(is_ppl);
			if ($(this).hasClass('move') && num == 1) {
	            tipsInfo('商品数量不能再减少了');
	            return false;
	        }

	        if ($(this).hasClass('move')) {
	        	num = num - 1 <= 1 ? 1 : num - 1;
				js_price -=  price;
				js_zhekou -= price * zhekou;
	        } else if ($(this).hasClass('add')) {
	        	if( is_ppl == 0 ){
	        		num = num + 1;
					js_price +=  price;
					js_zhekou += price * zhekou;
	        	}else{
	        		tipsInfo('该商品属于限购产品，每个ID限购一件！');
	        	}
	        }
	        $.get("{:U('update_num')}", {id: did, num: num}, function(r){
				if(r.status){
					obj.html(num);
					//总金额
					$('.ui-cart-goods-amount span b').html('￥' + js_price.toFixed(2));
					//折扣
					$('.ui-goods-cart-discount').html('(立奖：'+ js_zhekou.toFixed(2) +')');
				}	
			}, 'json');

		});



		DEL.tap(function(){
		    var dia=$.dialog({
		        title:'温馨提示',
		        content:'是否确定删除该商品？',
		        button:["确认","取消"]
		    });
		    var obj = $(this);
		    dia.on("dialog:action",function(e){
		    	if(e.index == 0){
			       	var did = Number(obj.attr('did'));
		            $.ajax({
		                url: "{:U('del_goods')}",
		                type: 'POST',
		                dataType: 'json',
		                data: {
		                    id: did
		                },
		                cache: false,
		                timeout: 50000,
		                error: function (data) {}, success: function (data) {
		                    tipsInfo('删除成功！');
		                    if($('.ui-list-goods-cart li').length <= 1){
		                    	$('.ui-list-goods-cart').html('<li>'+
															'<section class="ui-not-goods-wrap">'+
																'<div class="ui-cart-not-goods">'+
																    '<i class="iconfont">&#xf620;</i>'+
																    '<p>没有商品噢(ㄒoㄒ)~~</p>'+
																    '<div class="ui-btn-group">'+
																        '<a class="ui-btn-primary ui-btn" onclick="goHome();">去选购</a>'+
																    '</div>'+
															    '</div>'+
															'</section>'+
														'</li>');
		                    }
							if($('.ui-list-goods-cart li').length <= 1){
								//总金额
								$('.ui-cart-goods-amount span b').html('￥0.00');
								//折扣
								$('.ui-goods-cart-discount').html('(立奖：0.00');
							}else{
								var zhekou = Number(obj.attr('zhekou'))/100*0.4;
								var price = parseFloat(obj.attr('price'));
								var num = Number(obj.attr('num'));
								js_price -=  price * num;
								js_zhekou -= price * num * zhekou;
								
								//总金额
								$('.ui-cart-goods-amount span b').html('￥' + js_price.toFixed(2));
								//折扣
								$('.ui-goods-cart-discount').html('(立奖：' + js_zhekou.toFixed(2));
							}
							obj.parent().parent().remove();
		                }
		            }); 
	        	}
		    });
		    dia.on("dialog:hide",function(e){
		        console.log("dialog hide")
		    });
		});
		
		$('#cartCount').tap(function(){
			if( $('.checked').length > 0 ){
				$('#cartForm').submit();	
			}else{
				tipsInfo('请选者一项结算！');
			}
		});

		//确认选中
		$('.ui-list-goods-cart li').tap(function(){
			var lib = $(this).find('label input');
			lib.parent().addClass('checked').parent().siblings().find('label').removeClass('checked');	
			$('#id').val(lib.val());

			var obj = $(this).find('.ui-list-goods-price i');
			var zhekou = Number(obj.attr('zhekou'))/100*0.4;
			var price = parseFloat(obj.attr('price'));
			var num = Number(obj.attr('num'));
			var new_js_price =  price * num;
			var new_js_zhekou = price * num * zhekou;
			
			//总金额
			$('.ui-cart-goods-amount span b').html('￥' + new_js_price.toFixed(2));
			//折扣
			$('.ui-goods-cart-discount').html('(立奖：' + new_js_zhekou.toFixed(2) + ')');
		});

		function tipsInfo(msg){
			$.tips({
		        content:msg,
		        stayTime:2000,
		        type:"success"
		    })
		}

		function goHome(){
			window.location.href="{:U('Index/sindex')}";
		}
	</script>
</block>